/*
 *  Copyright (C) 2010 MADSA.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author:  Victor Mayoral Vilches
 *          
 */

package com.madsa.tuPeli.cartelera;

import com.sisof.R;

import android.os.Bundle;
import android.widget.TextView;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

public class PeliculasActivity extends ListActivity 
{
    

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setListAdapter(new SpeechListAdapter(this));        
    }
        

    protected void onListItemClick(ListView l, View v, int position, long id) 
    {    
       ((SpeechListAdapter)getListAdapter()).toggle(position);
    }
    
    private class SpeechListAdapter extends BaseAdapter {	
    	private Context mContext;
    	private String[] mTitles = getResources().getStringArray(R.array.titulos);
        private String[] mDialogue = getResources().getStringArray(R.array.contenidos);
        
        // Define if some items will be expanded or not
        private boolean[] mExpanded = 
        {
                false,
                false,
                false,
                false,
                false,
                false,
                false,
                false   
        };
        	
        public SpeechListAdapter(Context context)
        {
            mContext = context;
        }

        public int getCount() {
            return mTitles.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            SpeechView sv;
            if (convertView == null) {
                sv = new SpeechView(mContext, mTitles[position], mDialogue[position], mExpanded[position]);
            } else {
                sv = (SpeechView)convertView;
                sv.setTitle(mTitles[position]);
                sv.setDialogue(mDialogue[position]);
                sv.setExpanded(mExpanded[position]);
            }
            
            return sv;
        }

        public void toggle(int position) {
            mExpanded[position] = !mExpanded[position];
            notifyDataSetChanged();
        }
    }
    
    private class SpeechView extends LinearLayout {
        public SpeechView(Context context, String title, String dialogue, boolean expanded) {
            super(context);            
            this.setBackgroundColor(Color.TRANSPARENT);
            this.setOrientation(VERTICAL);
            this.setPadding(20, 20, 20, 20);
            
            
            // Here we build the child views in code. They could also have
            // been specified in an XML file.
            
            mTitle = new TextView(context);
            mTitle.setText(title);
            mTitle.setTextSize(20);
            mTitle.setTextColor(Color.WHITE);
            
            addView(mTitle, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
            		LayoutParams.WRAP_CONTENT));
            
            mDialogue = new TextView(context);
            mDialogue.setText(dialogue);            
            mDialogue.setPadding(0, 30, 0, 0);
            mDialogue.setBackgroundColor(Color.TRANSPARENT);
            addView(mDialogue, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            
            mDialogue.setVisibility(expanded ? VISIBLE : GONE);
        }
        
        public void setTitle(String title) {
            mTitle.setText(title);
        }
        
        public void setDialogue(String words) {
            mDialogue.setText(words);
        }
        
        public void setExpanded(boolean expanded) {
            mDialogue.setVisibility(expanded ? VISIBLE : GONE);
        }
        
        private TextView mTitle;
        private TextView mDialogue;
    }
}